package reduce.fz.com.reduce.activity;

import android.app.Service;
import android.content.Intent;
import android.os.CountDownTimer;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.TypeReference;

import java.util.HashMap;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.abstracts.AbResponseListener;
import reduce.fz.com.reduce.abstracts.AbResponseListener3;
import reduce.fz.com.reduce.bean.result.Result;
import reduce.fz.com.reduce.constant.BizConst;
import reduce.fz.com.reduce.constant.ParamConst;
import reduce.fz.com.reduce.service.UserService;
import reduce.fz.com.reduce.util.HttpUtils;
import reduce.fz.com.reduce.util.InputMethodUtil;
import reduce.fz.com.reduce.util.PatternUtil;
import reduce.fz.com.reduce.util.ServiceBuilder;

public class ResetPwdActivity extends BaseToolBarActivity implements View.OnClickListener {

    private String serverVcode;

    //登录手机号
    private EditText mMobile;
    //验证码
    private EditText mVcode;
    //密码
    private EditText mPassword;
    //发送验证码
    private LinearLayout mLlSendVcode;
    //下一步
    private TextView mTvReset;
    //获取验证码描述
    private TextView mTvSendTitle;

    private UserService userService = ServiceBuilder.builderUserService(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reset_pwd);
    }

    /**
     *
     */
    @Override
    public void initWidget() {
        mMobile = (EditText) this.findViewById(R.id.et_mobile);
        mPassword = (EditText) findViewById(R.id.et_password);
        mLlSendVcode = (LinearLayout) findViewById(R.id.ll_send_vcode);
        mTvReset = (TextView) this.findViewById(R.id.tv_reset);
        mTvSendTitle = (TextView) findViewById(R.id.tv_send_title);
        mVcode = (EditText) findViewById(R.id.et_vcode);
    }

    @Override
    public void initListener() {
        mLlSendVcode.setOnClickListener(this);
        mTvReset.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        String mobile = mMobile.getText().toString();
        String password = mPassword.getText().toString();
        switch (view.getId()) {
            case (R.id.ll_send_vcode):
                if (checkMobile()) {
                    sendSms(mobile);
                }
                break;
            case R.id.tv_reset:
                reset();
            default:
                break;
        }

    }

    public boolean submitCheck() {
        String code = mVcode.getText().toString();
        String password = mPassword.getText().toString();

        if (!checkMobile()) return false;
        if (!serverVcode.equals(code)) {
            toastMsg(getResources().getString(R.string.toast_vcode_error));
            InputMethodUtil.reuestFocus(mMobile);
            return false;
        }
        if (TextUtils.isEmpty(password)) {
            toastMsg(getResources().getString(R.string.toast_password_empty));
            InputMethodUtil.reuestFocusAndShowInput(mPassword);
            return false;
        }
        if (password.length() <= 6) {
            toastMsg(getResources().getString(R.string.toast_passwrd_pattern_error));
            InputMethodUtil.reuestFocusAndShowInput(mPassword);
            return false;
        }
        return true;
    }

    public Boolean checkMobile() {
        if (TextUtils.isEmpty(mMobile.getText())) {
            toastMsg(getResources().getString(R.string.toast_mobile_novalid));
            InputMethodUtil.reuestFocusAndShowInput(mMobile);
            return false;
        }
        if (!PatternUtil.isMobile(mMobile.getText().toString())) {
            toastMsg(getResources().getString(R.string.toast_mobile_novalid));
            InputMethodUtil.reuestFocusAndShowInput(mMobile);
            return false;
        }
        return true;
    }

    /**
     * 切换获取验证码可点击状态
     */
    public void clickAbleState() {
        mLlSendVcode.setClickable(true);
        mTvSendTitle.setText(getResources().getString(R.string.hint_send_vcode));
    }

    /**
     * 重新获取验证码倒计时状态
     */
    public void countState() {
        mLlSendVcode.setClickable(false);
        new CountDownTimer(60000, 1000) {
            @Override
            public void onTick(long l) {
                mTvSendTitle.setText(getResources().getString(R.string.hint_resend_vcode) + (l / 1000) + ")");
            }

            @Override
            public void onFinish() {
                clickAbleState();
            }
        }.start();
    }


    public void sendSms(String mobile) {
        userService.sendVcode(mobile, BizConst.VCODE_RESETPWD, new AbResponseListener<String>(new TypeReference<Result<String>>() {
        }) {
            @Override
            public void responseInfo(Result<String> data) {
                serverVcode = data.getData();
                countState();
            }
        });
    }

    public void reset() {

        if (!submitCheck()) return;
        String mobile = mMobile.getText().toString();
        String password = mPassword.getText().toString();
        userService.resetPassword(mobile, password, new AbResponseListener(new TypeReference<Result>() {
        }) {
            @Override
            public void responseInfo(Result data) {
                forwardLogin();

            }
        });
    }
    public void forwardLogin() {
        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
    }

}
